// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.chat.defaultChats;

import java.nio.ByteBuffer;
import zombie.Lua.LuaEventManager;
import zombie.characters.IsoPlayer;
import zombie.chat.ChatBase;
import zombie.chat.ChatManager;
import zombie.chat.ChatMessage;
import zombie.chat.ChatSettings;
import zombie.chat.ChatTab;
import zombie.chat.ServerChatMessage;
import zombie.core.Color;
import zombie.core.Core;
import zombie.core.network.ByteBufferWriter;
import zombie.debug.DebugLog;
import zombie.network.GameClient;
import zombie.network.chat.ChatType;

public class ServerChat extends ChatBase {
    public ServerChat(ByteBuffer bb, ChatTab tab, IsoPlayer owner) {
        super(bb, ChatType.server, tab, owner);
        this.setSettings(getDefaultSettings());
    }

    public ServerChat(int id, ChatTab tab) {
        super(id, ChatType.server, tab);
        this.setSettings(getDefaultSettings());
    }

    public static ChatSettings getDefaultSettings() {
        ChatSettings chatSettings = new ChatSettings();
        chatSettings.setBold(true);
        chatSettings.setFontColor(new Color(0, 128, 255, 255));
        chatSettings.setShowAuthor(false);
        chatSettings.setShowChatTitle(true);
        chatSettings.setShowTimestamp(false);
        chatSettings.setAllowColors(true);
        chatSettings.setAllowFonts(false);
        chatSettings.setAllowBBcode(false);
        return chatSettings;
    }

    public ChatMessage createMessage(String author, String text, boolean isAlert) {
        ChatMessage chatMessage = this.createMessage(text);
        chatMessage.setAuthor(author);
        if (isAlert) {
            chatMessage.setServerAlert(true);
        }

        return chatMessage;
    }

    public ServerChatMessage createServerMessage(String text, boolean isAlert) {
        ServerChatMessage serverChatMessage = this.createServerMessage(text);
        serverChatMessage.setServerAlert(isAlert);
        return serverChatMessage;
    }

    @Override
    public short getTabID() {
        return !GameClient.bClient ? super.getTabID() : ChatManager.getInstance().getFocusTab().getID();
    }

    @Override
    public ChatMessage unpackMessage(ByteBuffer bb) {
        ChatMessage chatMessage = super.unpackMessage(bb);
        chatMessage.setServerAlert(bb.get() == 1);
        chatMessage.setServerAuthor(bb.get() == 1);
        return chatMessage;
    }

    @Override
    public void packMessage(ByteBufferWriter b, ChatMessage msg) {
        super.packMessage(b, msg);
        b.putBoolean(msg.isServerAlert());
        b.putBoolean(msg.isServerAuthor());
    }

    @Override
    public String getMessagePrefix(ChatMessage msg) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(this.getChatSettingsTags());
        boolean _boolean = false;
        if (this.isShowTitle()) {
            stringBuilder.append("[").append(this.getTitle()).append("]");
            _boolean = true;
        }

        if (!msg.isServerAuthor() && this.isShowAuthor()) {
            stringBuilder.append("[").append(msg.getAuthor()).append("]");
            _boolean = true;
        }

        if (_boolean) {
            stringBuilder.append(": ");
        }

        return stringBuilder.toString();
    }

    @Override
    public String getMessageTextWithPrefix(ChatMessage msg) {
        return this.getMessagePrefix(msg) + " " + msg.getText();
    }

    @Override
    public void showMessage(ChatMessage msg) {
        this.messages.add(msg);
        if (this.isEnabled()) {
            LuaEventManager.triggerEvent("OnAddMessage", msg, this.getTabID());
        }
    }

    @Override
    public void sendMessageToChatMembers(ChatMessage msg) {
        for (short _short : this.members) {
            this.sendMessageToPlayer(_short, msg);
        }

        if (Core.bDebug) {
            DebugLog.log("New message '" + msg + "' was sent members of chat '" + this.getID() + "'");
        }
    }
}
